class HomeGame extends egret.Sprite{
	public HomeArr=[];//存储房子
	
	public HintArr=[];//提示条
	
	private BarrArr=[];//存取障碍物
	
	public speed = 4;//移动速度
	
	public Tooltip=RES.getRes("green_png");
	
	public TooltipShade=RES.getRes("hui_png");//提示条阴影
	
	private ImgErr=[];//僵尸死亡动画

	private ImgOk=[];//僵尸成功动画

	private BarrierArr=["shan_png","tree_png"];//障碍物资源
	
	public timer:egret.Timer = new egret.Timer(2200,0);//定时器创建房子
	
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE,this.init,this);
	};

	// 初始化
	private init(){
		// 随机数a用来选择房子的左右方向 0:left左 1:right右
		var a = Math.floor(Math.random()*2+0);
		
		//选择障碍物资源
		var b = Math.floor(Math.random()*2+0);

		//障碍物
		var barrObj =  new egret.Bitmap();
		
		//僵尸
		var homeObj =  new egret.MovieClip();

		// 提示条
		var hintObj =  new egret.Bitmap();
		
		//障碍物
		barrObj.texture = RES.getRes(this.BarrierArr[b]);
		

		barrObj.width =Math.ceil( barrObj.texture.textureWidth / 2);//障碍物宽度
		barrObj.height = Math.ceil(barrObj.texture.textureHeight / 2);//障碍物高度

		//提示条
		hintObj.texture = this.TooltipShade;
		hintObj.width = hintObj.texture.textureWidth / 2;
		hintObj.height = barrObj.height;

		// 僵尸
		var dataUrl = RES.getRes("jiangshi_json");
		var text:egret.Texture = RES.getRes("jiangshi_png");
		var jiangshi:egret.MovieClipDataFactory = new egret.MovieClipDataFactory( dataUrl, text );
		homeObj = new egret.MovieClip( jiangshi.generateMovieClipData( "jiangshi" ) );

		// 原始尺寸僵尸
		if(this.HomeArr.length == 0){
			homeObj.y = 0 - homeObj.height;
		}else{
			homeObj.y = this.HomeArr[this.HomeArr.length-1].home.y-this.HintArr[this.HomeArr.length-1].height - barrObj.height
		}
		barrObj.y=homeObj.y - barrObj.height / 4;
		hintObj.y=barrObj.y;
		hintObj.x = (this.stage.stageWidth - hintObj.width) / 2 ;
		if(a == 0){  //左
			homeObj.scaleX=-1;
			homeObj.x=0+homeObj.width;
			// 判断翻转 根据房子大小判断障碍物的所需时间
			barrObj.x=this.stage.stageWidth - barrObj.width;
		}else{ //右
			homeObj.x=this.stage.stageWidth - homeObj.width;
			barrObj.scaleX = -1;
			barrObj.x=0+barrObj.width;
		};
		this.addChild(homeObj);
		this.addChild(barrObj);
		this.addChild(hintObj);

		let Obj = {
			home:homeObj,//僵尸
			isTrue:false,//控制是否正确
			objX:a,//左边或者右边
		}
	
		this.HomeArr.push(Obj);
		this.BarrArr.push(barrObj);
		this.HintArr.push(hintObj);
		
		homeObj.gotoAndPlay("jump",-1);
	};
	// 创建房子定时器
	public HoustTime() {
		this.timer.addEventListener(egret.TimerEvent.TIMER,this.init,this);
		this.timer.start();
	};
	// 启动房子移动帧动画 -- 60帧
	public starTime(){
		this.addEventListener(egret.Event.ENTER_FRAME, this.creaHoust,this);
	};
	// 房子位移动画
	public creaHoust() {

		for(var i =0;i<this.HomeArr.length;i++){
			this.HomeArr[i].home.y += this.speed;
			this.BarrArr[i].y += this.speed;
			this.HintArr[i].y += this.speed;
			if (this.HomeArr[i].home.y >= this.stage.stageHeight){
				if(this.HomeArr[i].home.parent != null){
					this.HomeArr[i].home.parent.removeChild(this.HomeArr[i].home);
					this.BarrArr[i].parent.removeChild(this.BarrArr[i]);
					this.HintArr[i].parent.removeChild(this.HintArr[i]);
				}
				this.HomeArr.splice(i,1);
				this.BarrArr.splice(i,1);
				this.HintArr.splice(i,1);
			}	
		}
			
	};
	// 结束动画
	public end(){
		this.timer.stop();//停止创建房子
		this.removeEventListener(egret.Event.ENTER_FRAME,this.creaHoust,this);//移除动画
	}
}

